package com.lostandfound.action;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;
import org.springframework.beans.factory.annotation.Autowired;

import com.lostandfound.entity.ItemType;
import com.lostandfound.service.ItemService;
import com.lostandfound.util.ReturnCode;
import com.opensymphony.xwork2.ActionSupport;

@ParentPackage("basePackage")
@Namespace("/")
@Results({  
    @Result(type = "json", params = {"root", "res"})  
}) 
public class ItemAction extends ActionSupport {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	@Autowired
	private ItemService itemService;
	
	private ItemType itemType;//对象模型
	private Map<String, Object> res = new HashMap<String, Object>();//返回json信息
	private HashMap<String, Object> pagemap;//分页信息
	
	public ItemType getItemType() {
		return itemType;
	}
	public void setItemType(ItemType itemType) {
		this.itemType = itemType;
	}
	
	public Map<String, Object> getRes() {
		return res;
	}
	public void setRes(Map<String, Object> res) {
		this.res = res;
	}
	
	public HashMap<String, Object> getPagemap() {
		return pagemap;
	}
	public void setPagemap(HashMap<String, Object> pagemap) {
		this.pagemap = pagemap;
	}
	
	/**
	 * 添加物品大类
	 * @return
	 */
	@Action(value="saveItem")
	public String saveItem(){
		if(this.itemType != null){
			try {
				this.itemType.setIstatus("0");
				itemService.save(this.itemType);
				res.put("code", ReturnCode.SUCCESS);//添加成功
			} catch (Exception e) {
				System.out.println("**添加物品大类失败**");
				res.put("code", ReturnCode.FAIL);
			}
		}else{
			res.put("code", ReturnCode.LOST_NEWS);//添加信息不能为空
		}
		System.out.println("item");
		return SUCCESS;
	}
	
	/**
	 * 修改产品大类信息
	 * @return
	 */
	@Action(value="updateItem")
	public String updateItem(){
		if(this.itemType != null){
			try {
				ItemType nowItem = itemService.selectByPrimary(this.itemType.getIid());
				nowItem.setIname(this.itemType.getIname());
				itemService.update(nowItem);
				res.put("code", ReturnCode.SUCCESS);
			} catch (Exception e) {
				System.out.println("**修改物品大类失败**");
				res.put("code", ReturnCode.FAIL);
			}
		}else{
			res.put("code", ReturnCode.LOST_NEWS);//修改后信息不能为空
		}
		return SUCCESS;
	}
	
	/**
	 * 删除一个或者多个物品大类信息
	 * @return
	 */
	@Action(value="deleteItem")
	public String deleteItem(){
		if(this.res != null && this.res.get("ids") != null){
			String[] ids = (String[])this.res.get("ids");
		
			String iids = ids[0]; 
			iids = iids.substring(1, iids.length()-1);
			String[] id = iids.split(",");
			if(id != null && id.length >0){
				Long[] i_ids = new Long[id.length];
				for (int i=0;i<id.length;i++) {
					Long l1 = Long.parseLong(id[i]);
					i_ids[i] = l1;
				}
				try {
					itemService.deleteItemByBranch(i_ids);//删除物品大类
					this.res.put("code", ReturnCode.SUCCESS);//删除成功
				} catch (Exception e) {
					System.out.println("**删除物品大类信息失败！**");
					this.res.put("code", ReturnCode.FAIL);
				}
			}
		}else{
			this.res.put("code", ReturnCode.LOST_NEWS);//要删除的集合ID不能为空
		}
		return SUCCESS;
	}
	
	/**
	 * 根据ID查询信息
	 * @return
	 */
	@Action(value="selectItemByPrimary")
	public String selectItemByPrimary(){
		if(this.itemType != null){
			try {
				ItemType item = itemService.selectByPrimary(this.itemType.getIid());
				res.put("item", item);
				res.put("code", ReturnCode.SUCCESS);
			} catch (Exception e) {
				System.out.println("**获取物品大类详情失败!**");
				res.put("code", ReturnCode.FAIL);
			}
		}else{
			res.put("code", ReturnCode.LOST_NEWS);//要查询的信息ID不能为空
		}
		return SUCCESS;
	}
	
	/**
	 * 分页获取物品大类列表
	 * @return
	 */
	@Action(value="listItemByPage")
	public String listItemByPage(){
		if(pagemap == null){//pagemap存放分页属性，如果pagemap为空，设置默认每页显示10条数据
			pagemap = new HashMap<String, Object>();
			pagemap.put("page", "1");
			pagemap.put("size", "10");
		}
		try {
			@SuppressWarnings("unchecked")
			List<ItemType> itemTypes = (List<ItemType>)itemService.listItemByPage(pagemap);//分页查询板块信息
			int count = itemService.getCount();
			this.res.put("itemTypes", itemTypes);//获取当页数据
			this.res.put("itemcount",count);//获取总条目
			int size = Integer.parseInt((String)pagemap.get("size"));
			this.res.put("totalpage",count%size>0?count/size+1:count/size);//获取总页数
			this.res.put("code", ReturnCode.SUCCESS);//查询成功
		} catch (Exception e) {
			System.out.println("查询失败！");
			this.res.put("code", ReturnCode.FAIL);
		}
		return SUCCESS;
	}
	
	/**
	 * 获取所有的物品大类
	 * @return
	 */
	@Action(value="getAllItem")
	public String getAllItem(){
		try {
			@SuppressWarnings("unchecked")
			List<ItemType> itemTypes = (List<ItemType>)itemService.getAllItem();
			res.put("itemTypes", itemTypes);
			res.put("code", ReturnCode.SUCCESS);
		} catch (Exception e) {
			System.out.println("查询失败！");
			this.res.put("code", ReturnCode.FAIL);
		}
		return SUCCESS;
	}
	

}
